<?php

namespace App\Console\Commands\OpenShop\Renew;

use App\Models\Renew\RenewOrder\RenewDeferOperateHistoryModel;
use App\Remote\Rms\RmsShopRemote;
use App\Service\Cruise\Shop\CruiseShopService;
use App\Service\OpenShop\Feishu\ConfigService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Kuafu\Nsq\NsqPublisher;

class CrontabCloseOrderGoodsSystemCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'renew:crontabCloseOrderGoodsSystem';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '续签延期：定时轮询 到期关闭订货系统';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $deferHistoryModel = RenewDeferOperateHistoryModel::query()
                                                          ->where("status", RenewDeferOperateHistoryModel::STATUS_0)
                                                          ->groupBy("shopId")
                                                          ->select([
                                                              DB::raw("max(id) as mid"),
                                                              "shopId"
                                                          ])->get();

        RenewDeferOperateHistoryModel::query()->whereIn("id", $deferHistoryModel->pluck("mid")->toArray())->get()
                                     ->each(function (RenewDeferOperateHistoryModel $deferOperateHistoryModel) {
                                         if ($deferOperateHistoryModel->endTime <= time()) {
                                             //停收银
                                             (new NsqPublisher(config('nsq.default.address')))
                                                 ->pub('tiger.openShop.shopStatus.switch', [
                                                     'nsqEventTime'  => time(),
                                                     'shopId'        => $deferOperateHistoryModel->shopId,
                                                     'switch'        => 0, // 门店禁用关闭为 0
                                                     'operatorId'    => 0,
                                                     'operatorName'  => "系统",
                                                     'reason'        => "续签延期订货到期关闭订货功能",
                                                     'operationTime' => time()
                                                 ]);

                                             // 自动停收银
                                             RmsShopRemote::updateShopFieldById(
                                                 $deferOperateHistoryModel->shopId,
                                                 ['cashRegisterStatus' => RmsShopRemote::SHOP_DEFER_EXPIRED_OFF]
                                             );

                                             RenewDeferOperateHistoryModel::query()
                                                                          ->where("shopId",
                                                                              $deferOperateHistoryModel->shopId)
                                                                          ->where("status",
                                                                              RenewDeferOperateHistoryModel::STATUS_0)
                                                                          ->update([
                                                                              "updatedAt" => time(),
                                                                              "status"    => RenewDeferOperateHistoryModel::STATUS_2
                                                                          ]);
                                         }
                                     });

        return 0;
    }
}
